package com.lzb.newloan;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.menu.ResideMenu;
import com.lzb.newloan.utils.ActivityList;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;

import java.util.Timer;
import java.util.TimerTask;


public class InvestPersonActivity extends BaseActivity implements OnClickListener {

    private LinearLayout layoutLeftMenu;
    private ResideMenu resideMenu;
    private ImageView index_img;
    private TextView index_txt;
    private ImageView person_img;
    private TextView person_txt, index_left_menu_txt_name;
    private ImageView more_img;
    private TextView more_txt;
    private android.app.AlertDialog phoneDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index2);
        initView();

        Intent intert = getIntent();
        int id = intert.getIntExtra("id", 0);
        if(id==1){
            changeFragment(new InvestOutMoneyFragment());
            index_img.setImageResource(R.drawable.invest_icon_nor);
            index_txt.setTextColor(getResources().getColor(R.color.textcolor));
            person_img.setImageResource(R.drawable.myinvest_chosed);
            person_txt.setTextColor(getResources().getColor(R.color.yellor));
            more_img.setImageResource(R.drawable.more_gray);
            more_txt.setTextColor(getResources().getColor(R.color.textcolor));
        }else{
            changeFragment(new InvestFragment());
            index_img.setImageResource(R.drawable.invest_icon_chosed);
            index_txt.setTextColor(getResources().getColor(R.color.yellor));
            person_img.setImageResource(R.drawable.myinvest_nor);
            person_txt.setTextColor(getResources().getColor(R.color.textcolor));
            more_img.setImageResource(R.drawable.more_gray);
            more_txt.setTextColor(getResources().getColor(R.color.textcolor));
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    private void initView() {
        resideMenu = new ResideMenu(this);
        resideMenu.setBackground(R.color.blue);
        resideMenu.attachToActivity(this);

        // resideMenu.setScaleValue(0.6f);
        // 禁止使用右侧菜单
        resideMenu.setDirectionDisable(ResideMenu.DIRECTION_RIGHT);
        LayoutInflater mInflater = LayoutInflater.from(this);
        layoutLeftMenu = resideMenu.getLayoutLeftMenu();

        View convertView = mInflater.inflate(R.layout.activity_index_left2,
                layoutLeftMenu);
        convertView.findViewById(R.id.contact_rela).setOnClickListener(this);
        convertView.findViewById(R.id.invite_rela).setOnClickListener(this);
        convertView.findViewById(R.id.market_rela).setOnClickListener(this);
        convertView.findViewById(R.id.linear_problem).setOnClickListener(this);
        tv_refere = (TextView) convertView.findViewById(R.id.tv_refere);
        tv_refere.setOnClickListener(this);
        resideMenu.setMenuListener(new ResideMenu.OnMenuListener() {
            @Override
            public void openMenu() {
            }

            @Override
            public void closeMenu() {
            }
        });

        LinearLayout index_home = (LinearLayout) findViewById(R.id.index_home);
        index_home.setOnClickListener(this);
        LinearLayout index_person = (LinearLayout) findViewById(R.id.index_person);
        index_person.setOnClickListener(this);
        LinearLayout index_more = (LinearLayout) findViewById(R.id.index_more);
        index_more.setOnClickListener(this);
        index_img = (ImageView) findViewById(R.id.index_img);
        index_txt = (TextView) findViewById(R.id.index_txt);
        person_img = (ImageView) findViewById(R.id.person_img);
        person_txt = (TextView) findViewById(R.id.person_txt);
        more_img = (ImageView) findViewById(R.id.more_img);
        more_txt = (TextView) findViewById(R.id.more_txt);
        convertView.findViewById(R.id.linear_aboutus).setVisibility(View.GONE);

//		if (BaseApplication.isInvest) {
        index_img.setImageResource(R.drawable.invest_icon_chosed);
        index_txt.setText("投资列表");
        person_img.setImageResource(R.drawable.myinvest_nor);
        person_txt.setText("我的放款");
//		}

        ImageView img_contect = (ImageView) convertView.findViewById(R.id.img_contect);
        img_contect.setImageResource(R.drawable.invest_menu_complany);
        tv_refere.setText("切换到贷款");
        index_left_menu_txt_name = (TextView) convertView.findViewById(R.id.index_left_menu_txt_name);
        SharedPreferences sp = getApplicationContext().getSharedPreferences("config", 0x0000);
        String phone = sp.getString("phone", "");
        index_left_menu_txt_name.setText(phone);
    }

    public void changeFragment(Fragment targetFragment) {
        resideMenu.clearIgnoredViewList();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.index_fragment, targetFragment, "fragment")
                .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.index_home:
                changeFragment(new InvestFragment());
                index_img.setImageResource(R.drawable.invest_icon_chosed);
                index_txt.setTextColor(getResources().getColor(R.color.yellor));
                person_img.setImageResource(R.drawable.myinvest_nor);
                person_txt.setTextColor(getResources().getColor(R.color.textcolor));
                more_img.setImageResource(R.drawable.more_gray);
                more_txt.setTextColor(getResources().getColor(R.color.textcolor));
                break;
            case R.id.index_person:
                changeFragment(new InvestOutMoneyFragment());
                index_img.setImageResource(R.drawable.invest_icon_nor);
                index_txt.setTextColor(getResources().getColor(R.color.textcolor));
                person_img.setImageResource(R.drawable.myinvest_chosed);
                person_txt.setTextColor(getResources().getColor(R.color.yellor));
                more_img.setImageResource(R.drawable.more_gray);
                more_txt.setTextColor(getResources().getColor(R.color.textcolor));
                break;
            case R.id.index_more:
//                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
                changeFragment(new InvestMoreFragment());
                index_img.setImageResource(R.drawable.invest_icon_nor);
                index_txt.setTextColor(getResources().getColor(R.color.textcolor));
                person_img.setImageResource(R.drawable.person_gray);
                person_txt.setTextColor(getResources().getColor(R.color.textcolor));
                more_img.setImageResource(R.drawable.more_blue);
                more_txt.setTextColor(getResources().getColor(R.color.yellor));
                break;
            case R.id.contact_rela://
                this.phoneDialog = new android.app.AlertDialog.Builder(this)
                        .create();
                LayoutInflater lay = LayoutInflater
                        .from(((Context) InvestPersonActivity.this));
                final View inflate = lay.inflate(R.layout.layout_dialog2,
                        (ViewGroup) null);
                inflate.findViewById(R.id.cancle).setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        InvestPersonActivity.this.phoneDialog.dismiss();
                    }
                });

                final TextView textView = (TextView) inflate
                        .findViewById(R.id.dialog_ok);
                ((TextView) inflate.findViewById(R.id.dialog_cancle))
                        .setOnClickListener((View.OnClickListener) new View.OnClickListener() {
                            public void onClick(final View view) {
                                InvestPersonActivity.this.phoneDialog.dismiss();
                            }
                        });
                inflate.findViewById(R.id.wx_problem).setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // 打开微信
                                InvestPersonActivity.this.phoneDialog.dismiss();
                                try {
                                    Intent intent = new Intent(Intent.ACTION_MAIN);
                                    ComponentName cmp = new ComponentName(
                                            "com.tencent.mm",
                                            "com.tencent.mm.ui.LauncherUI");
                                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    intent.setComponent(cmp);
                                    startActivity(intent);
                                } catch (ActivityNotFoundException e) {
                                    Toast.makeText(InvestPersonActivity.this,
                                            "检查到您手机没有安装微信，请安装后使用该功能",
                                            Toast.LENGTH_LONG).show();
                                }

                            }
                        });
                inflate.findViewById(R.id.qq_problem).setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // 打开扣扣进行客服沟通
                                InvestPersonActivity.this.phoneDialog.dismiss();
                                try {
                                    String url = "mqqwpa://im/chat?chat_type=wpa&uin=3321662529";
                                    startActivity(new Intent(Intent.ACTION_VIEW,
                                            Uri.parse(url)));
                                } catch (ActivityNotFoundException e) {
                                    Toast.makeText(InvestPersonActivity.this,
                                            "检查到您手机没有安装QQ，请安装后使用该功能",
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                inflate.findViewById(R.id.tel_problem).setOnClickListener(
                        new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // 拨打电话
                                InvestPersonActivity.this.phoneDialog.dismiss();
                                InvestPersonActivity.this.startActivity(new Intent(
                                        Intent.ACTION_DIAL, Uri
                                        .parse("tel:075582341685")));

                            }
                        });
                textView.setOnClickListener((View.OnClickListener) new View.OnClickListener() {
                    public void onClick(final View view) {
                    }
                });
                this.phoneDialog.show();
                this.phoneDialog.setCanceledOnTouchOutside(false);
                this.phoneDialog.setContentView(inflate);


                break;
            case R.id.invite_rela:
                startActivity(new Intent(getApplicationContext(),
                        InviteActivity.class));
                break;
            case R.id.market_rela:
                Intent intent2 = new Intent(InvestPersonActivity.this,
                        MoreMarketActivity.class);
                startActivity(intent2);
                break;
            case R.id.linear_problem:
                Intent intent = new Intent(InvestPersonActivity.this, ProblemActivity.class);
                intent.putExtra("title", "常见问题");
                intent.putExtra("url", Config.COMMON_PROBLEM);
                startActivity(intent);
                break;
            case R.id.tv_refere:
                startActivity(new Intent(this, IndexActivity.class));
                break;
            default:
                break;
        }
    }

    private boolean isExit = false;
    private TimerTask timeTask;
    private Timer timer = new Timer();
    private TextView tv_refere;

    // 监听返回键是否退出
    @Override
    public void onBackPressed() {
        if (isExit) {
            ActivityList.tuichu();
            this.finish();
            System.exit(0);
        } else {
            isExit = true;
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
            timeTask = new TimerTask() {

                @Override
                public void run() {
                    isExit = false;
                }
            };
            timer.schedule(timeTask, 3000);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }

    @Override
    public void processMessage(Message message) {
        // TODO Auto-generated method stub

    }
}
